package com.xmb.wechat.delegate;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SizeUtils;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.squareup.picasso.Picasso;
import com.xmb.wechat.R;
import com.xmb.wechat.baserecyclerview.BaseRecyclerViewManager;
import com.xmb.wechat.bean.PYQBean;
import com.xmb.wechat.bean.PYQCommentsBean;
import com.xmb.wechat.bean.PYQPicBean;
import com.xmb.wechat.bean.PYQTrendsBean;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.picasso.PicassoPYQPicSizeFitTransform;

import java.util.ArrayList;
import java.util.List;

import io.objectbox.relation.ToMany;

/**
 * @Author lijiangop
 * @CreateTime 2020/5/12 17:49
 */
public class PYQDelegate extends BaseRecyclerViewManager {

    private PYQBean entity;

    public PYQDelegate(Activity activity, PYQBean entity) {
        super(activity);
        this.entity = entity;
    }

    @Override
    public BaseQuickAdapter initAdapter() {
        return new PYQAdapter(new ArrayList<>());
    }

    private class PYQAdapter extends BaseMultiItemQuickAdapter<PYQTrendsBean, BaseViewHolder> {

        /**
         * Same as QuickAdapter#QuickAdapter(Context,int) but with
         * some initialization data.
         *
         * @param data A new list is created out of this one to avoid mutable list
         */
        public PYQAdapter(List data) {
            super(data);
            addItemType(0, R.layout.item_pyq);
        }

        @Override
        protected void convert(BaseViewHolder helper, PYQTrendsBean item) {
            if (item.getItemType() == 0) {
                //头像,昵称
                WechatContactBean sender = WechatContactBean.getWechatContactBean(context, item.getSenderId(), false);
                if (sender != null) {
                    WechatContactBean.setupAvatarIntoImageView(sender, helper.getView(R.id.iv_owner));
                    helper.setText(R.id.tv_avatar, TextUtils.isEmpty(sender.getName()) ? "" : sender.getName());
                }
                //文字内容
                TextView tvContent = helper.getView(R.id.tv_content);
                if (TextUtils.isEmpty(item.getContent()))
                    tvContent.setVisibility(View.GONE);
                else {
                    tvContent.setVisibility(View.VISIBLE);
                    tvContent.setText(TextUtils.isEmpty(item.getContent()) ? "" : item.getContent());
                }
                //图片
                FrameLayout flPic = helper.getView(R.id.fl_pic);
                ToMany<PYQPicBean> pics = item.getPics();
                if (pics.size() > 1) {//有一条空数据（加号）
                    flPic.setVisibility(View.VISIBLE);
                    showPicDelegate(pics, helper.getView(R.id.iv_pic), helper.getView(R.id.rv_pic));
                } else {
                    flPic.setVisibility(View.GONE);
                }
                //位置
                TextView tvLocation = helper.getView(R.id.tv_location);
                if (item.isShowLocation()) {
                    tvLocation.setVisibility(View.VISIBLE);
                    tvLocation.setText(item.getLocationCity() + "·" + item.getLocationDetail());
                } else {
                    tvLocation.setVisibility(View.GONE);
                }
                //时间
                helper.setText(R.id.tv_time, TextUtils.isEmpty(item.getTime()) ? "" : item.getTime());
                //删除按钮
                if (entity != null) {
                    WechatContactBean owner = WechatContactBean.getWechatContactBean(context, entity.getOwnerId(), false);
                    TextView tvDelete = helper.getView(R.id.tv_delete);
                    if (owner != null && sender != null && sender.getId() == owner.getId()) {
                        tvDelete.setVisibility(View.VISIBLE);
                        helper.getView(R.id.tv_delete).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                removeItem(mData.indexOf(item));
                            }
                        });
                    } else {
                        tvDelete.setVisibility(View.GONE);
                    }
                }
                //点赞评
                String likes = item.getLikes();
                ToMany<PYQCommentsBean> comments = item.getComments();
                LinearLayout llComments = helper.getView(R.id.ll_comments);
                if (TextUtils.isEmpty(likes) && comments.size() == 0) {
                    llComments.setVisibility(View.GONE);
                } else {
                    llComments.setVisibility(View.VISIBLE);
                    //点赞
                    if (!TextUtils.isEmpty(likes)) {
                        helper.setVisible(R.id.ll_likes, true);
                        helper.setText(R.id.tv_likes, item.getLikes());
                    } else {
                        helper.setVisible(R.id.ll_likes, false);
                        helper.setVisible(R.id.split, false);
                    }
                    //评论
                    if (comments.size() > 0) {
                        helper.setVisible(R.id.rv_comments, true);
                        new PYQCommentsDelegate(context, helper.getView(R.id.rv_comments)).addData(comments);
                    } else {
                        helper.setVisible(R.id.rv_comments, false);
                        helper.setVisible(R.id.split, false);
                    }
                }
            }
        }

        private void showPicDelegate(ToMany<PYQPicBean> pics, ImageView imageView, RecyclerView recyclerView) {
            if (pics.size() == 2) {//有一条空数据（加号）
                Picasso.get().load("file://" + pics.get(0).getPic()).transform(new PicassoPYQPicSizeFitTransform(SizeUtils.dp2px(205)))
                        .error(R.drawable.default_image).into(imageView);
                imageView.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.GONE);
            } else {
                imageView.setVisibility(View.GONE);
                recyclerView.setVisibility(View.VISIBLE);
                PYQPicDelegate pyqPicDelegate = new PYQPicDelegate(context, recyclerView);
                pyqPicDelegate.addData(pics);
            }
        }
    }

    @Override
    protected RecyclerView.ItemDecoration getItenDecoration() {
        return null;
    }
}
